$(function () {
    $(".paginate>ul").on("click", "li", function (e) {
        e.preventDefault();
        let val = $(this).text().trim();
        let pageNo = parseInt($("#page-no").text());
        let pageSize = parseInt($("#pageSize").val());
        let pages = parseInt($("#pages").text());
        if ($(this).hasClass("first")) {
            search(1, pageSize);
        } else if ($(this).hasClass("last")) {
            search(pages, pageSize);
        } else if ($(this).hasClass("next")) {
            pageNo++;
            if (pageNo > pages) {
                pageNo = pages;
            }
            search(pageNo + 1, pageSize);
        } else if ($(this).hasClass("prev")) {
            pageNo--;
            if (pageNo < 1) {
                pageNo = 1;
            }
            search(pageNo, pageSize);
        } else {
            search(parseInt(val), pageSize);
        }
    });
    //修改
    $("#tbl>tbody").on("click",":button[name = edit]",function (){
        let id =   $(this).closest("tr").children().eq(1).text();
        location.href=ctx + "/food/edit?id=" + id;
    });
    //删除
    $("#tbl>tbody").on("click",":button[name = del]",function (){
        let delUrl = ctx + "/food/delete";
        let delData = {};
        let ids =  $(this).closest("tr").children().eq(1).text();
        // layer.msg(ids);
        if(ids.trim() !== ""){
            delData.ids = ids;
        }

       $.post(delUrl, delData,function(resp){
           // layer.msg("回调");
           let pageNo = parseInt($("#page-no").text());
           let pageSize = parseInt($("#pageSize").val());
            if(resp.success){
                layer.alert("删除成功",function(){
                   location.reload();
                    search(pageNo,pageSize);
                });

            }else {
                layer.alert("删除失败，请稍后重试");
                search(pageNo,pageSize);
            }
       });
    });
    $("#export-btn").click(function (){
        location.href=ctx + "/food/export";

    });
    $("#search-btn").click(function () {
        let size = $("#pageSize").val();
        search(1, size);
    });
    $("#search-btn").trigger("click");
    //重置
    $("#reset-btn").click(function () {
        $(".search>form")[0].reset();
        $("#search-btn").trigger("click");
    });
    //全选
    $("#check-all").click(function () {
        let checked = $(this).prop("checked");
        $("tbody>tr>td:first-child>input").prop("checked", checked);
    });
    //航选中
    $("#tbl>tbody").on("click", "td:not(:first-child)", function () {
        let $tr = $(this).parent();
        //console.log($tr);
        let check = $tr.children().eq(0).children();

        check.prop("checked", !check.prop("checked"));

    });

    $("#add-btn").click(function (){
        let addUrl = ctx +"/food/add";
        location.href=addUrl;
    });
    $("#del-btn").click(function () {
        let $checks = $("#tbl>tbody>tr>td:first-child>:checked");
        if ($checks.length == 0) {
            layer.msg("请选中要删除的记录行");
        } else {
            let ids = [];
            $checks.each(function (idx, item) {
                let id = $(this).closest("tr").children().eq(1).text();
                ids.push(id);
            });
            if (ids.length > 0) {
                layer.confirm("是否确认删除", function(){
                    deleteByIds();
                });
            }

            function deleteByIds() {
                let url = ctx + "/food/delete";
                $.ajax(url, {
                    method: "post",
                    data: {
                        ids
                    }, dataType: "json",
                    traditional: true,
                    success:function (resp){
                        let pageNo = parseInt($("#page-no").text());
                        let pageSize = parseInt($("#pageSize").val());
                        if(resp.success) {
                            layer.alert(resp.msg || "删除成功",function (){
                                location.reload();
                                search(pageNo,pageSize);
                            });

                        }else{
                            layer.alert(resp.error||"删除失败，请稍后再试");
                        }
                    },
                    error:function (resp){
                         layer.alert(resp.error||"删除失败，请稍后再试");
                    }
                });

            }


        }


    });

});

function search(pageNo = 1, pageSize = 10) {

    let foodId = $(".search #food_id").val();
    let kindId = $(".search #kind_id").val();
    let name = $(".search #name").val();
    let price = $(".search #price").val();
    let description = $(".search #description").val();

    let data = {
        pageNo,
        pageSize
    };
    if (foodId.trim() !== "") {
        data.foodId = foodId;
    }
    if (kindId.trim() !== "") {
        data.kindId = kindId;
    }
    if (name.trim() !== "") {
        data.name = name;
    }
    if (price.trim() !== "") {
        data.price = price;
    }
    if (description.trim() !== "") {
        data.description = description;
    }
    let url = ctx + "/food/list";

    $.post(url, data, function (resp) {
        let elist = resp.elist;
        let pi = resp.pi;

        $("#pages").text(pi.pages);
        $("#total").text(pi.count);
        $("#page-no").text(pi.pageNo);
        $("#tbl>tbody").empty();
        for (let i = 0; i < elist.length; i++) {
            let emp = elist[i];
            let $tr = $("<tr></tr>");
            $tr.append($("<td><input type='checkbox'></td>"));
            $tr.append($("<td>"+emp.id+"</td>"));
            $tr.append($("<td>" + emp.foodId + "</td>"));
            $tr.append($("<td>" + emp.name + "</td>"));
            $tr.append($("<td>" + emp.kindId + "</td>"));
            $tr.append($("<td>" + emp.kindName + "</td>"));
            $tr.append($("<td>" + emp.price + "</td>"));
            $tr.append($("<td>" + (emp.description || "") + "</td>"));
            $tr.append($("<td><button name='edit'>修改</button></td>"));
            $tr.append($("<td><button name='del'>删除</button></td>"));
            $("#tbl>tbody").append($tr);

        }

        $(".paginate>ul>li:not(:nth-child(1)):not(:nth-child(2)):not(.next):not(.last)").remove();
        let navfirst = pi.navfirst;
        let navlast = pi.navlast;
        console.log(navfirst + " " + navlast);
        for (let i = navfirst; i < navlast; i++) {
            $li = $("<li><a href='#'>" + i + "</a> </li>");
            if (i == pi.pageNo) {
                $li.addClass("active")
            }
            $("li.next").before($li);
        }
    }, "json");
}